package cn.car91.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.car91.R;
import cn.car91.bean.Exposure;
import cn.car91.config.InitApplication;
import cn.car91.utils.ImageManager;

import com.lidroid.xutils.bitmap.PauseOnScrollListener;

public class ExposureCenterListAdapter extends ArrayAdapter {
    private int resource;
    private LayoutInflater layoutInflater;
    private List<Exposure> data;
    static ImageManager imageManager;

    public List<Exposure> getData() {
        return data;
    }

    public void setData(List<Exposure> data) {
        this.data = data;
    }

    public void addData(List<Exposure> data) {
        this.data.addAll(data);
    }

    private InitApplication config;

    public ExposureCenterListAdapter(Context context, int resource) {
        super(context, resource);
        // TODO Auto-generated constructor stub
        this.resource = resource;
        this.layoutInflater = LayoutInflater.from(context);
        this.data = new ArrayList<Exposure>();
        config = (InitApplication) context.getApplicationContext();

        imageManager = new ImageManager(getContext(), "sdcard/car91/temp/exposure");
    }

    public final class exposureItem {
        /*public TextView id;*/
        public ImageView image;
        public TextView title;
        public TextView name;
        public TextView time;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return data.get(arg0);
    }

    public Integer getExposureId(int which) {
    	return data.get(which).getEid();
    }
    
    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        exposureItem exposureItem = null;
        if (arg1 == null) {
            exposureItem = new exposureItem();

            arg1 = layoutInflater.inflate(resource, null);
            /*exposureItem.id = (TextView) arg1.findViewById(R.id.exposure_item_id);*/
            exposureItem.image = (ImageView) arg1.findViewById(R.id.exposure_item_pic);
            exposureItem.title = (TextView) arg1.findViewById(R.id.exposure_item_info_title);
            exposureItem.name = (TextView) arg1.findViewById(R.id.exposure_item_repairshop_name);
            exposureItem.time = (TextView) arg1.findViewById(R.id.exposure_item_time);
            arg1.setTag(exposureItem);
        } else {
            exposureItem = (exposureItem) arg1.getTag();
        }
        Exposure exposure = data.get(arg0);

        /*exposureItem.id.setText(String.valueOf(exposure.getEid()));*/

        imageManager.displayImage(config.getArticleServerUrl() + "/" + exposure.getPic(), exposureItem.image, getContext().getResources().getDrawable(R.drawable.exposure_default));
        ((ListView) arg2).setOnScrollListener(new PauseOnScrollListener(imageManager.getBitmapUtils(), true, true));

        exposureItem.title.setText(exposure.getTitle());
        exposureItem.name.setText(exposure.getName());
        exposureItem.time.setText(DateFormat.format("yy-MM-dd kk:mm", exposure.getTime()));
        return arg1;
    }
}
